package com.hj.dapp.business.ocr.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeBasicRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.hj.dapp.business.ocr.model.OcrDto;
import com.hj.dapp.business.ocr.model.OcrVo;
import com.hj.dapp.business.ocr.service.OcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/2
 */
@Service
public class OcrServiceImpl implements OcrService {
    @Autowired
    private Client client;
    @Override
    public OcrVo getIdCardInfo(OcrDto ocrDto) {
        RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest()
                .setOutputFigure(ocrDto.getIsOutputFigure())
                .setOutputQualityInfo(ocrDto.getIsOutputQualityInfo())
                .setUrl(ocrDto.getUrl());
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            RecognizeIdcardResponse response = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
            String data = response.getBody().getData();
            JSONObject result = JSON.parseObject(data, JSONObject.class);
            System.out.println(result);
            OcrVo ocrVo = result.getObject("data", OcrVo.class);
            System.out.println(ocrVo);
            return ocrVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
